package GoFile

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"m/model"
)



func InitDB() (db *gorm.DB, erre error){
	var err error
	//driverName := viper.GetString("datasource.driverName")
	//host := viper.GetString("datasource.host")
	//port := viper.GetString("datasource.port")
	//database := viper.GetString("datasource.database")
	//username := viper.GetString("datasource.username")
	//password := viper.GetString("datasource.password")
	//charset := viper.GetString("datasource.charset")

	args := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
		"postgres",
		"gsj123",
		"MyTraining",
		"localhost",
		"5432",
	)
	db, err = gorm.Open(postgres.Open(args), &gorm.Config{})
	if err != nil {
		panic("连接数据库失败" + err.Error())
		return db,erre
	}
	fmt.Println("数据库连接成功！")
	db.AutoMigrate(&model.Student{})
	return db, erre
}

func InitRedisDB() (conn redis.Conn, err error)  {
	db, err := redis.Dial("tcp", "127.0.0.1:6379")
	if err != nil {
		fmt.Println("连接redis失败！, ", err.Error())
		return
	}
	return db, err
}
